/*
 * jQuery mypager plug-in 1.1
 *
 * http://www.tsky.net/
 *
 * Copyright (c) 2010 tsky.net
 *
 * create by cainiao   2010-06-18
 * 
 * update by cainiao   2010-06-29
 */
 
(function($){
	$.fn.mypager = function(opts){
		var options = {};
		var pager = $(this);
		$.extend(options, $.fn.mypager.defaults, opts);
		
		function doAjaxPage(page){
			var params = options.queryParams||{};
			params.page = page;
			params.rows = options.rows;
			$.ajax({
				url: options.url,
				type: "post",
				dataType: "json",
				data: options.queryParams,
				async: false,
				cache: false,
				data: params,
				success: function(json){
					createGrid(json);
				}
			});
		}
		
		// 初始化
		doAjaxPage(options.page);
		
		function createGrid(jsonData){
			pager.html("");// 清除内容
			
			// 数据内容
			var dataShowDiv = $('<div></div>');
			// 数据列表
			var mypagerDataDiv = $('<div class="mypager-data"></div>');
			
			if(jsonData.total == 0){
				dataShowDiv.text('很抱歉，目前没有任何记录!');
				dataShowDiv.css("color", "red");
				dataShowDiv.css("text-align", "center");
				
				mypagerDataDiv.html("");// 清除内容
				mypagerDataDiv.append(dataShowDiv);
				
				pager.append(mypagerDataDiv);
			}else{
				// 数据内容
				for(var i in jsonData.rows){
					var dataShowDiv_ul = $('<ul></ul>');
					var jsonObj = jsonData.rows[i];
					var columns = options.columns;
					for(var k in columns){
					    // alert(eval("jsonObj."+columns[k].field));
					    var dataShowDiv_ul_li = $('<li></li>');
					    dataShowDiv_ul_li.addClass(columns[k].fClass);
					    
					    // var dataHref = $('<a title="'+ jsonObj[columns[k].field] +'">'+ jsonObj[columns[k].field] +'</a>');
					    // var dataHref = $('<a title="'+ jsonObj["createTime"] +'">'+ jsonObj[columns[k].field] +'</a>');
					    var dataHref = $('<a title="news" date="'+ jsonObj["createTime"] +'">'+ jsonObj[columns[k].field] +'</a>');
					    
					    if(columns[k].isHref && isNew(jsonObj["createTime"])){
					    	// dataHref.append('<span style="color: red;">&nbsp;NEW</span>');
					    	dataHref.append('<img src="jslib/mypager/news.gif"/>');
			   			}
					    
					    if(columns[k].isHref){
					    	dataHref.attr("value", jsonObj["id"]);
					    	dataHref.attr("href", "javascript:void(0)");
					    	dataHref[0].onclick = eval(columns[k].handler||function(){});
					    }
					    dataShowDiv_ul_li.append(dataHref);
					    dataShowDiv_ul.append(dataShowDiv_ul_li);
					}
					dataShowDiv.append(dataShowDiv_ul);
				}
				
				mypagerDataDiv.html("");// 清除内容
				mypagerDataDiv.append(dataShowDiv);
				
				pager.append(mypagerDataDiv);
				
				// 分页导航条
			    var paginationHtml = $('<table width="100%" style="padding: 0px;"></table>');
			    var paginationTR = $('<tr></tr>');
			    
			    //1.跳转
			    var paginationTD_jump = $('<td class="mypager_jump">转至第 </td>');
			    var paginationTD_jump_select = $('<select id="jump_select" name="page"></select>');
			    var select_value = '';
			    for(var i = 1; i <= jsonData.pages; i++){
			    	select_value += '<option value="'+ i +'">'+ i +'</option>';
			    }
			    paginationTD_jump_select.html(select_value);
			    paginationTD_jump_select.val(jsonData.page);
			    paginationTD_jump_select.change(function(){
					doAjaxPage($(this).val());
			    });
			    
			    //2.首页、上一页、下一页、尾页
			    var paginationTD_pager = $('<td></td>');
			    //首页
			    var pagination_first = $('<a style="padding: 5px;">[首页]</a>');
			   	//上一页
			    var pagination_up = $('<a style="padding: 5px;">[上一页]</a>');
			    
			    if(jsonData.pages >= 1 && jsonData.page != 1){
			    	pagination_first.attr("href", "javascript:void(0)");
			    	pagination_up.attr("href", "javascript:void(0)");
			    	
			    	pagination_first.click(function(){
			    		doAjaxPage(1);
			    	});
			    	
			    	pagination_up.click(function(){
						doAjaxPage(jsonData.page - 1);
			    	});
			    }
			    
			    //3.数字分页
			    var pagination_num = $('<span></span>');
			    var numsize = $.fn.mypager.defaults.numsize;
			    if(jsonData.pages <= numsize){
				    for(var i = 1; i <= jsonData.pages; i++){
				    	var pagination_num_href = $('<a href="javascript:void(0)" style="padding: 5px;"></a>');
				    	pagination_num_href.attr("val", i);
				    	pagination_num_href.text('['+ i +']');
				    	if(jsonData.page == i){
				    		pagination_num_href.addClass("mypager-curentpage");
				    	}
				    	pagination_num_href.click(function(){
							doAjaxPage($(this).attr("val"));
				    	});
				    	pagination_num.append(pagination_num_href);
				    }
			    }else{
			    	var beginPage = jsonData.page;
			    	var endPage = jsonData.pages;
			    	if(jsonData.page + numsize < jsonData.pages){
			    		endPage = jsonData.page + numsize;
			    	}
			    	if(jsonData.page + numsize > jsonData.pages){
			    		beginPage = jsonData.pages - numsize;
			    	}
			    	if(jsonData.page - numsize <= 0){
			    		beginPage = 1;
			    		endPage = 1 + numsize;
			    	}
			    	for(var i = beginPage; i <= endPage; i++){
				    	var pagination_num_href = $('<a href="javascript:void(0)" style="padding: 5px;"></a>');
				    	pagination_num_href.attr("val", i);
				    	pagination_num_href.text('['+ i +']');
				    	if(jsonData.page == i){
				    		pagination_num_href.addClass("mypager-curentpage");
				    	}
				    	pagination_num_href.click(function(){
							doAjaxPage($(this).attr("val"));
				    	});
				    	pagination_num.append(pagination_num_href);
				    }
			    }
			    
			    //下一页
			    var pagination_next = $('<a style="padding: 5px;">[下一页]</a>');
			    //尾页
			    var pagination_last = $('<a style="padding: 5px;">[尾页]</a>');
			    
			    if(jsonData.page != jsonData.pages){
			    	pagination_next.attr("href", "javascript:void(0)");
			    	pagination_last.attr("href", "javascript:void(0)");
			    	
			    	pagination_next.click(function(){
						doAjaxPage(jsonData.page + 1);
			    	});
			    	
			    	pagination_last.click(function(){
						doAjaxPage(jsonData.pages);
			    	});
			    }
			    
			    //分页信息
			    var pagination_info = $('<a style="padding: 5px;">当前 '+ jsonData.page +' / 共 '+ jsonData.pages +' 页</a>');
				
				paginationTD_pager.append(pagination_first);
				paginationTD_pager.append(pagination_up);
				paginationTD_pager.append(pagination_num);
				paginationTD_pager.append(pagination_next);
				paginationTD_pager.append(pagination_last);
				paginationTD_pager.append(pagination_info);
				
				paginationTD_jump.append(paginationTD_jump_select);
				paginationTD_jump.append(" 页");
				
				paginationTR.append(paginationTD_jump);
				paginationTR.append(paginationTD_pager);
				
				paginationHtml.append(paginationTR);
				
				// 分页导航条
				pager.append(paginationHtml);
				
				// document.write(pager.html());
			}
			
		}
	};
	
	$.fn.mypager.defaults = {
		numsize: 4,// 数字分页大小
		url: null,// URL地址
		page: 1,// 当前页
		rows: 10,// 分页大小
		columns: null,// 显示的列
		queryParams: null// 查询参数
	};
	
})(jQuery);